package com.example.client.rpc;

import com.example.api.api.common.RpcRequest;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author heyuduo
 * @date 2022/6/18 16:33
 */
@Slf4j
public class RpcInvocationHandler implements InvocationHandler {

    private String host;

    private int port;

    public RpcInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {

        log.info("客户端开始封装参数【RpcRequest】");
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setParameterTypes(method.getParameterTypes());
        rpcRequest.setParameters(args);
        log.info("ClassName:{}", rpcRequest.getClassName());

        log.info("客户端开始获取传输工具【tcpTransport】");
        TcpTransport tcpTransport = new TcpTransport(host, port);

        log.info("客户端开始发送请求参数【RpcRequest】");
        return tcpTransport.send(rpcRequest);
    }
}
